package com.cnbot.aimolib.recognize;

import android.graphics.RectF;
import android.graphics.Region;

import com.cnbot.aimolib.bean.AimoOutlineData;
import com.cnbot.aimolib.bean.AimoRawFaceData;
import com.cnbot.aimolib.db.AimoFaceDbData;
import com.cnbot.aimolib.utils.DetectionTrackerUtils;
import com.white.loglib.L;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author ytf
 * Created by on 2021/06/08 15:52
 */
public class DefaultRecognizeStrategy implements IRecognizeStrategy {


    private Region region = new Region();

    @Override
    public AimoFaceDbData matchFace(List<AimoRawFaceData> list, List<AimoFaceDbData> users,
                                    List<AimoOutlineData> outlines, RectF previewRect,
                                    float threshold) {
        int size = list.size();
        if(size == 1){
            for (AimoFaceDbData user : users){
                float score = DetectionTrackerUtils.calculateSimilarity(user.feature, list.get(0).faceFeature.getFeatures());
                if(score >= threshold){
                    L.e("相似度：" + score);
                    return user;
                }
            }
        }else{
            //选最大的
            Collections.sort(outlines, new Comparator<AimoOutlineData>() {
                @Override
                public int compare(AimoOutlineData me, AimoOutlineData other) {
                    float areaMe = me.faceRect.width() * me.faceRect.height();
                    float areaOther = other.faceRect.width() * other.faceRect.height();
                    return Float.compare(areaMe, areaOther);
                }
            });
            AimoOutlineData outline =  outlines.get(size-1);
            //判断五点是否在预览视图里面
            region.set((int)previewRect.left, (int)previewRect.top, (int)previewRect.right, (int)previewRect.bottom);
            boolean r1 = region.contains((int)outline.leftEye.x, (int)outline.leftEye.y);
            boolean r2 = region.contains((int)outline.rightEye.x, (int)outline.rightEye.y);
            boolean r3 = region.contains((int)outline.nose.x, (int)outline.nose.y);
            boolean r4 = region.contains((int)outline.rightMouth.x, (int)outline.rightMouth.y);
            boolean r5 = region.contains((int)outline.leftMonth.x, (int)outline.leftMonth.y);
            if(r1 && r2 && r3 && r4 && r5){
                AimoRawFaceData sample =  findById(list, outline.faceId);
                //比较相似度
                for (AimoFaceDbData user : users){
                    float score = DetectionTrackerUtils.calculateSimilarity(user.feature, sample.faceFeature.getFeatures());
                    if(score >= threshold){
                        L.e("相似度：" + score);
                        return user;
                    }
                }
            }
        }
        return null;
    }

    private AimoRawFaceData findById(List<AimoRawFaceData> list, int id){
        for (AimoRawFaceData a : list){
            if(id == a.faceInfo.getId())
                return a;
        }
        return null;
    }
}
